package com.ayoka.parenting.ui.coach.studentmanager;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.RadioGroup;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CommonFragmentPagerAdapter;
import com.ayoka.parenting.bean.BasicUserInfoBean;
import com.ayoka.parenting.bean.UserInfoDetailBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.databinding.ActivityStudentManagerBinding;
import com.ayoka.parenting.interf.SimplePageChangeListener;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.ayoka.parenting.ui.coach.myprofit.MyProfitActivity;
import com.ayoka.parenting.ui.coach.sendinvitecode.SendInviteCodeActivity;
import com.ayoka.parenting.ui.grown.list.GrownListFragment;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: StudentManagerActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class StudentManagerActivity extends MVPBaseActivity<ActivityStudentManagerBinding, StudentManagerContract.View, StudentManagerPresenter> {

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, StudentManagerActivity.class);
        activity.startActivity(intent);
    }

    private CommonFragmentPagerAdapter pageAdapter;

    private int index = 0;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_student_manager;
    }

    @Override
    protected void initViews() {
        mDataBinding.rgStudent.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == mDataBinding.rbCareStudent.getId()) {
                    index = 0;
                } else if (checkedId == mDataBinding.rbManagerStudent.getId()) {
                    index = 1;
                }
                mDataBinding.viewPager.setCurrentItem(index);
            }
        });
        mDataBinding.viewPager.addOnPageChangeListener(new SimplePageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    mDataBinding.rgStudent.check(mDataBinding.rbCareStudent.getId());
                } else if (position == 1) {
                    mDataBinding.rgStudent.check(mDataBinding.rbManagerStudent.getId());
                }
            }
        });

        mDataBinding.btnSendInvite.setOnClickListener(this);
        mDataBinding.btnMyProfit.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.my_student);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        configCoachInfo();
        configFragments();
    }

    private void configCoachInfo() {
        if (UserManager.getInstance().detailBean != null) {
            UserInfoDetailBean detailBean = UserManager.getInstance().detailBean;
            if (detailBean.user_info != null) {
                if (detailBean.user_info.BasicInfo != null) {
                    BasicUserInfoBean basicUserInfoBean = detailBean.user_info.BasicInfo;
                    ImageLoader.getInstance().displayImage(basicUserInfoBean.LocalAvatar, mDataBinding.ivCoachHeader, ImageLoaderOptions.getCircleOptions());
                    mDataBinding.tvCoachName.setText(basicUserInfoBean.WeixinNickName);
                    mDataBinding.tvCoachType.setText(UserManager.getCoachType(1));
                }
            }
        }
    }

    @Override
    protected StudentManagerPresenter getPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        if (view == mDataBinding.btnSendInvite) {
            SendInviteCodeActivity.start(this);
        } else if (view == mDataBinding.btnMyProfit) {
            MyProfitActivity.start(this);
        }
    }

    public void configFragments() {
        pageAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager());

        // 关心学员
        StudentManagerListFragment careStudentFragment = StudentManagerListFragment.newInstance(Constants.ManagerType.CARE);
        pageAdapter.addFragment(careStudentFragment, getString(R.string.care_student));

        // 管理学员
        StudentManagerListFragment managerStudentFragment = StudentManagerListFragment.newInstance(Constants.ManagerType.MANAGER);
        pageAdapter.addFragment(managerStudentFragment, getString(R.string.manager_student));

        mDataBinding.viewPager.setAdapter(pageAdapter);
        mDataBinding.viewPager.setCurrentItem(index);
        mDataBinding.viewPager.setOffscreenPageLimit(pageAdapter.getCount());
    }
}
